﻿using LBCMS.BLL;
using LBCMS.EntityFrameWork.FrameModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using static LBCMS.BLL.PredicateBuilder;

namespace LBCMS.Controllers
{
    public class ActivityController : Controller
    {
        EntityFrameWork.BLLContext db = new EntityFrameWork.BLLContext();

        public ActionResult Index()
        {
            //var years = db.TW_Activity.Select(p => p.year).Distinct().OrderBy(p => p).ToList();
            //string tYear = DateTime.Now.Year.ToString();
            //if (!years.Contains(tYear))
            //    years.Add(tYear);
            //ViewBag.Year = years;
            return View();
        }

        public string getPageData(queryCondition where, int page, int limit)
        {
            return new BLL.frameHelper().getData<TW_Activity>(where, page, limit, new OrderModelField() { IsDESC = true, PropertyName = "id" });
        }

        public ActionResult Data(string rowGuid)
        {
            //模式
            if (Request["id"] != null && Request["id"].ToString() != "")
            {
                ViewBag.Mode = "edit";
            }

            return View();
        }

        public string Delete(string ids)
        {
            string[] arrIds = ids.Split(';');
            foreach (var item in arrIds)
            {
                if (item != "")
                {
                    var data = new TW_Activity();
                    data.id = Convert.ToInt32(item);
                    db.TW_Activity.Attach(data);
                    db.Entry(data).State = System.Data.Entity.EntityState.Deleted;
                }
            }

            db.SaveChanges();

            return Helper.JsonHelper.toSuccessJson();
        }

        public string getData(int id)
        {
            return Helper.JsonHelper.toJson(new EntityComm().GetSingleById<TW_Activity>(id));
        }
        public string Save(TW_Activity data)
        {
            if (data.id > 0)
            {
                db.TW_Activity.Attach(data);
                db.Entry(data).State = System.Data.Entity.EntityState.Modified;
            }
            else
            {
                db.TW_Activity.Add(data);
            }

            db.SaveChanges();
            return Helper.JsonHelper.toSuccessJson();
        }
    }
}
